package com.yhh;

import com.yhh.Entity.Command;
import com.yhh.Entity.HandlerCommand;
import com.yhh.Entity.MyFile;
import com.yhh.Exceptions.InstructionException;
import com.yhh.Exceptions.NonInstructionException;

import java.net.Socket;
import java.util.ArrayList;
import java.util.Map;

import static com.yhh.Pipeline.pipeline;

public class ShowCommand extends Command implements HandlerCommand {
    protected ShowCommand(Socket socket) {
        super(socket);
        command.add("-a");
    }

    @Override
    public void handlerParameter(Map<String, String> args) throws InstructionException, NonInstructionException {

    }

    @Override
    public boolean check(Map<String, String> args) throws NonInstructionException, InstructionException {
        return true;
    }

    @Override
    public void handlerCommand(Map<String, String> args) throws InstructionException, NonInstructionException {
        handlerShow();
    }
    private void handlerShow() {
        ArrayList<MyFile> myFiles = new ArrayList<>();
        //提取该ip下的所有文件
        pipeline.forEach((key, fileRes) -> {
            if (fileRes.getIp().equals(socket.getInetAddress().toString())) {
                myFiles.add(fileRes);
            }
        });
        //发送以counts:开头的数据
        sendMsg("counts:" + myFiles.size());
        System.out.println(myFiles.size());
        myFiles.forEach(myfile -> {
            sendMsg(myfile.toString());
        });
    }

}
